/*
 * @Description: 
 * @Date: 2020-12-07 14:59:46
 * @LastEditors: lother
 * @LastEditTime: 2021-01-11 14:47:02
 * @FilePath: \AccountOpening-H5\src\assets\js\common.js
 */
import router from '@/router';
import { flow  } from '@/api/common'
export default{
    install(Vue, options) {
        Vue.prototype.$changeData = function (){
                // alert('执行成功');
        };
        Vue.prototype.$search = function (){
            var links = {};
            location.search.replace(/([^?&=]+)=([^&]+)/g, (_, k, v) => links[k] = v);
            links;
            return links;
        };
        Vue.prototype.$query = function (){
            return router.currentRoute.query 
        };
        Vue.prototype.$evalreg = function (val){
            val = val || "";
            return RegExp(val);
        };
        Vue.prototype.$closest = function(el, selector) {
            var matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
            while (el) {
                if (matchesSelector.call(el, selector)) {
                    break;
                }
                el = el.parentElement;
            }
            return el;
        },
        Vue.prototype.$backTitle = function (text,href,show){
            var htmlStr   = text;
            href = href || 'pactList'
            show = show??true;
            var toHref = (router.mode=='hash'?'#/'+href:'/'+href)
            var reg0 = new RegExp("#.*?#", "g");
            var array = htmlStr.match(reg0);
            let content = htmlStr;
            if(array && array.length !==0){
                for (let index = 0; index < array.length; index++) {
                    const element = array[index];
                    var  str = element.replace(/#/g,'')
                    if(show){
                        content = content.replace(element,`<a href="${toHref}"  class="blueColor">${str}</a>`)
                    }else{
                        content = content.replace(element,`<span  class="blueColor">${str}</span>`)
                    }
                }
            }
            return content
        };
        Vue.prototype.$routerTo = function(url,params){
            var  list  ={}
            var urlHref = (url.split('?'))[0]
            url.replace(/([^?&=]+)=([^&]+)/g, (_, k, v) => list[k] = v);
            list = Object.assign({},list,params)
            router.replace({
                name:urlHref,
                query:list
            })
        },
        // 当前页面
        Vue.prototype.$pageFlow = function(){
            var userType = localStorage.getItem('userType') ?? ''
            var isModify =  localStorage.getItem('isModify') ?? 0
            var pageId = localStorage.getItem('pageId')??''
            var obj = {pageId,userType,isModify}
            return new Promise((resolve, reject) => {
                flow(obj).then(res => {
                    if(res.code==104){
                        location.reload()
                    }else if(res.code==102 || res.code==103){
                        router.replace({
                            path:'examine',
                        })
                    }

                  resolve(res)
                }).catch(error => {
                  reject(error)
                })
              })
        },
        //上个页面
        Vue.prototype.$prevPage = function({pageId}){
            var userType = localStorage.getItem('userType') || ''
            pageId = pageId || '';
            var isModify = localStorage.getItem('isModify') ?? 0
            var obj = {pageId,userType,isModify}
            return new Promise((resolve, reject) => {
                flow(obj).then(res => {
                    localStorage.setItem('pageId',pageId)
                    resolve(res)
                    // Vue.prototype.$page(res,'prev')

                    // if(res.code==1){
                    //     var page_info = res.data.page_info
                    //     localStorage.setItem('page',res.data.prev_page)
                    //     if( res.data.current_page== res.data.prev_page){
                    //         Vue.prototype.$routerTo('documentType')
                    //         return false
                    //     }
                    //     if(page_info.type==1 ){
                    //         Vue.prototype.$routerTo(page_info.url_path)
                    //     }else if(page_info.type==2){
                    //         router.replace({
                    //             path:'custom',
                    //             query:{id:page_info.id}
                    //         })
                    //     }
                    // }else if(res.code==102){
                    //     router.replace({
                    //         path:'examine',
                    //     })
                    // }
                }).catch(error => {
                  reject(error)
                })
            })
        },
        // 下个页面
        Vue.prototype.$nextPage = function({pageId,show}){
            var StepsPage = JSON.parse(localStorage.getItem('StepsPage') )|| {};
            if(JSON.stringify(StepsPage)!=='{}' && StepsPage.next_id == ''){
                router.replace({
                    path:'examine',
                })
                return false;
            }
            localStorage.removeItem('pageId')
            var userType = localStorage.getItem('userType') || ''
            var isModify = localStorage.getItem('isModify') ?? 0
            var obj = {userType,isModify}
            show = show ?? false;
            return new Promise((resolve, reject) => {
                flow(obj).then(res => {
                    // localStorage.setItem('pageId',pageId)
                    if(show){
                        resolve(res)
                    }else{
                        Vue.prototype.$page(res,'next')
                    }
                }).catch(error => {
                  reject(error)
                })
              })
        },
        Vue.prototype.$page = function(res,type){
            
            if(localStorage.getItem('isModify')==2 || res.code==102){
                router.replace({
                    path:'examine',
                })
                return false
            }
            if(res.code==1){
                var page_info = res.data.page_info
                // localStorage.setItem('pageId',page_info.id??'')
                // if(type=='next'){
                //     localStorage.setItem('page',res.data.current_page)
                // }else if(type=='prev'){
                //     localStorage.setItem('page',res.data.prev_page)
                // }
                // return false
                if(localStorage.getItem('isModify')==1){
                    router.replace({
                        path:'custom',
                        query:{id:page_info.id}
                    })
                }
                if(page_info.type==1 ){
                    Vue.prototype.$routerTo(page_info.url_path)
                }else if(page_info.type==2){
                    router.replace({
                        path:'custom',
                        query:{id:page_info.id}
                    })
                }
            }
        },
        Vue.prototype.$Icon = function(href){
            href = href ?? JSON.parse(localStorage.getItem('common'))?.terminalIcon
            var link = document.createElement('link');
            link.type = 'image/x-icon';
            link.rel = 'shortcut icon';
            link.href = href;
            document.getElementsByTagName('head')[0].appendChild(link);
        }
    }
}